#include "discsettingswidget.h"

#include <QVBoxLayout>
#include <QGridLayout>
#include <QLabel>

#include <KButtonGroup>
#include <KUrl>
#include <KFileDialog>
#include <KLocale>
#include <KPushButton>

DiscSettingsWidget::DiscSettingsWidget( QWidget *aParent ) : QWidget( aParent ), pSettings( NULL )
{
	QVBoxLayout *mainLayout = new QVBoxLayout( this );
	KButtonGroup *group = new KButtonGroup( this );
	group->setTitle( i18n("Directories") );
	QVBoxLayout *vLayout = new QVBoxLayout( group );
	QGridLayout *gLayout = new QGridLayout;
	gLayout->addWidget( new QLabel( i18n("Output directory:") ), 0, 0 );
	pOutputLineEdit = new KLineEdit( this );
	pOutputLineEdit->setReadOnly( true );
	gLayout->addWidget( pOutputLineEdit, 0, 1 );
	KPushButton *btn = new KPushButton( this );
	btn->setText( ".." );
	connect( btn, SIGNAL( clicked() ), this, SLOT( setOutputPath() ) );
	gLayout->addWidget( btn, 0, 2 );
	gLayout->addWidget( new QLabel( i18n("Temporary directory:") ), 1, 0 );
	pTempLineEdit = new KLineEdit( this );
	pTempLineEdit->setReadOnly( true );
	gLayout->addWidget( pTempLineEdit, 1, 1 );
	btn = new KPushButton( this );
	btn->setText( ".." );
	connect( btn, SIGNAL( clicked() ), this, SLOT( setTempPath() ) );
	gLayout->addWidget( btn, 1, 2 );
	vLayout->addLayout( gLayout );
	vLayout->addStretch();
	mainLayout->addWidget( group );
	
	group = new KButtonGroup( this );
	vLayout = new QVBoxLayout( group );
	pDeleteTemp = new QCheckBox( i18n("Delete temporary files"), this );
	pDeleteInput = new QCheckBox( i18n("Delete input files"), this );
	pCreateStructure = new QCheckBox( i18n("Create disc structure ( only for DVD )"), this );
	vLayout->addWidget( pDeleteTemp );
	vLayout->addWidget( pDeleteInput );
	vLayout->addWidget( pCreateStructure );
	vLayout->addStretch();
	mainLayout->addWidget( group );

	mainLayout->addStretch();	
}

DiscSettingsWidget::~DiscSettingsWidget()
{

}

void DiscSettingsWidget::setSettings( KDCDiscSettings *aSettings )
{
	pSettings = aSettings;
	load();
}

void DiscSettingsWidget::save()
{
	if ( pSettings )
	{
		pSettings->setOutputPath( pOutputLineEdit->text() );
		pSettings->setTempPath( pTempLineEdit->text() );
		pSettings->setDeleteTempFiles( pDeleteTemp->isChecked() );
		pSettings->setDeleteOryginalFiles( pDeleteInput->isChecked() );
		pSettings->setCreateStructure( pCreateStructure->isChecked() );
	}
}

void DiscSettingsWidget::load()
{
	if ( pSettings )
	{
		pTempLineEdit->setText( pSettings->tempPath() );
		pOutputLineEdit->setText( pSettings->outputPath() );
		pDeleteTemp->setChecked( pSettings->deleteTempFiles() );
		pDeleteInput->setChecked( pSettings->deleteOryginalFiles() );
		pCreateStructure->setChecked( pSettings->createStructure() );
	}
}

void DiscSettingsWidget::setOutputPath()
{
	pOutputLineEdit->setText( KFileDialog::getExistingDirectory( KUrl( pOutputLineEdit->text() ), this, i18n("Choose output directory") ) );
}

void DiscSettingsWidget::setTempPath()
{
	pTempLineEdit->setText( KFileDialog::getExistingDirectory( KUrl( pTempLineEdit->text() ), this, i18n("Choose temporary directory") ) );
}

